#! /usr/bin/perl

use FindBin qw($Bin);
use lib "$Bin/../lib/";
use FileAIS;

unless (@ARGV==2) {
    print "perl $0 <in> <out>\n";
    exit;
}

my ($in,$out) = @ARGV;

my $in_hash = &FileAIS::Open2Hash($in,"GO");
#my @tmp = keys %{$in_hash};
#die "@tmp\n";

my $out_hash = &cvt($in_hash);

&FileAIS::Hash2File($out_hash,$out);

#######################/#######################
sub cvt{
    my ($in_hash) = @_;
    my %hash;

    my %tmp;
    foreach my $root (keys %{$in_hash}) {
        my $childs = $in_hash->{$root}->{'childrens'};
        my $organelle = $in_hash->{$root}->{'organelle'};
        my @childs = split(/,/,$childs);
        foreach my $child (@childs) {
            push @{$tmp{$child}},$organelle;
        }
    }

    foreach my $child (keys %tmp) {
        @organelles = @{$tmp{$child}};
        my $organelles = join(',',@organelles);
        $hash{$child}{'go'} = $child;
        $hash{$child}{'organelle'} = $organelles;
    }
    return \%hash;
}
